/**
 * 
 */
package com.maywide.jdp.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.maywide.jdp.core.exception.AppException;
import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.system.annotation.JSON;
import com.maywide.jdp.system.bean.RoleBean;
import com.maywide.jdp.system.entities.BusinessFunction;
import com.maywide.jdp.system.entities.Button;
import com.maywide.jdp.system.entities.Menu;
import com.maywide.jdp.system.entities.Module;
import com.maywide.jdp.system.entities.Operator;
import com.maywide.jdp.system.entities.Organization;
import com.maywide.jdp.system.entities.Role;
import com.maywide.jdp.system.entities.Shortcut;
import com.maywide.jdp.system.utils.JSONEditor;

/**
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Controller("roleService")
public class RoleServiceImpl implements RoleService {
	private static final String APP_TYPE_BASE = "base";
	private static final String APP_TYPE_OPERATORS = "operators";
	private static final String APP_TYPE_MENUS = "menus";
	private static final String APP_TYPE_SHORTCUTS = "shortcuts";
	private static final String APP_TYPE_BUTTONS = "buttons";
	private static final String APP_TYPE_FUNCTIONS = "functions";
	private static final String APP_TYPE_MODULES = "modules";

	/**
	 * 角色Bean接口
	 */
	private RoleBean bean = null;

	/**
	 * @param bean
	 *            the bean to set
	 */
	@Autowired
	@Qualifier("roleBean")
	public void setBean(RoleBean bean) {
		this.bean = bean;
	}

	@JSON
	@RequestMapping(value = "/system/role/insert.json")
	@Override
	public Role insert(@RequestParam(value = "role") Role role,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.insert(role);
		return role;
	}

	@JSON
	@RequestMapping(value = "/system/role/update.json")
	@Override
	public Role update(@RequestParam(value = "role") Role roleVO,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		String id = roleVO.getId();
		Role role = (Role) bean.load(id);
		// 取得不可改变属性
		Organization entity = role.getEntity();
		roleVO.setEntity(entity);

		BeanUtils.copyProperties(roleVO, role);

		bean.update(role);
		return role;
	}

	@JSON
	@RequestMapping(value = "/system/role/updateApp.json")
	@Override
	public Role updateApp(@RequestParam(value = "role") Role roleVO,
			@RequestParam(value = "appType") String appType,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		String id = roleVO.getId();

		if (appType.equals(APP_TYPE_BASE)) {
			// 取得不可改变属性
			Role role = (Role) bean.load(id);
			Organization parent = role.getEntity();
			List<Operator> operators = role.getOperators();

			roleVO.setEntity(parent);
			roleVO.setOperators(operators);
			BeanUtils.copyProperties(roleVO, role);

			bean.update(role);
			return role;
		} else if (appType.equals(APP_TYPE_OPERATORS)) {
			List<Operator> operators = roleVO.getOperators();
			Role role = (Role) bean.load(id);
			role.setOperators(operators);
			bean.update(role);
			return role;
		} else if (appType.equals(APP_TYPE_MENUS)) {
			List<Menu> menus = roleVO.getMenus();
			Role role = (Role) bean.load(id);
			role.setMenus(menus);
			bean.update(role);
			return role;
		} else if (appType.equals(APP_TYPE_SHORTCUTS)) {
			List<Shortcut> shortcuts = roleVO.getShortcuts();
			Role role = (Role) bean.load(id);
			role.setShortcuts(shortcuts);
			bean.update(role);
			return role;
		} else if (appType.equals(APP_TYPE_MODULES)) {
			List<Module> modules = roleVO.getModules();
			Role role = (Role) bean.load(id);
			role.setModules(modules);
			bean.update(role);
			return role;
		} else if (appType.equals(APP_TYPE_BUTTONS)) {
			List<Button> buttons = roleVO.getButtons();
			Role role = (Role) bean.load(id);
			role.setButtons(buttons);
			bean.update(role);
			return role;
		} else if (appType.equals(APP_TYPE_FUNCTIONS)) {
			List<BusinessFunction> functions = roleVO.getFunctions();
			Role role = (Role) bean.load(id);
			role.setFunctions(functions);
			bean.update(role);
			return role;
		} else {
			throw new AppException("未知的应用类型!");
		}
	}

	@JSON
	@RequestMapping(value = "/system/role/load.json")
	@Override
	public Role load(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Role role = (Role) bean.load(id);
		return role;
	}

	@RequestMapping(value = "/system/role/lock.json")
	@Override
	public void lock(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.lock(id);
	}

	@RequestMapping(value = "/system/role/unlock.json")
	@Override
	public void unlock(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.unlock(id);
	}

	@RequestMapping(value = "/system/role/move.json")
	@Override
	public void move(@RequestParam(value = "ids") List<String> ids,
			@RequestParam(value = "targetID") String targetID,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.move(ids, targetID);
	}

	@JSON
	@RequestMapping(value = "/system/role/list.json")
	public List<Role> list(
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false) String dir,
			@RequestParam(value = "buttonID", required = false) String buttonID,
			@RequestParam(value = "funID", required = false) String funID,
			@RequestParam(value = "menuID", required = false) String menuID,
			@RequestParam(value = "moduleID", required = false) String moduleID,
			@RequestParam(value = "operatorID", required = false) String operatorID,
			@RequestParam(value = "shortcutID", required = false) String shortcutID,
			@RequestParam(value = "active", required = false) String active,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();
		if (active != null)
			conditions.put("active", Boolean.parseBoolean(active));

		if (buttonID != null)
			conditions.put("buttonID", buttonID);

		if (funID != null)
			conditions.put("funID", funID);

		if (menuID != null)
			conditions.put("menuID", menuID);

		if (moduleID != null)
			conditions.put("moduleID", moduleID);

		if (operatorID != null)
			conditions.put("operatorID", operatorID);

		if (shortcutID != null)
			conditions.put("shortcutID", shortcutID);

		return bean.list(sort, dir, conditions, null);
	}

	@JSON
	@RequestMapping(value = "/system/role/page.json")
	public Page page(
			@RequestParam(value = "first", defaultValue = "0") int first,
			@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false) String dir,
			@RequestParam(value = "organizationID", required = false) String organizationID,
			@RequestParam(value = "buttonID", required = false) String buttonID,
			@RequestParam(value = "menuID", required = false) String menuID,
			@RequestParam(value = "moduleID", required = false) String moduleID,
			@RequestParam(value = "operatorID", required = false) String operatorID,
			@RequestParam(value = "shortcutID", required = false) String shortcutID,
			@RequestParam(value = "key", required = false) String key,
			@RequestParam(value = "active", required = false) String active,
			@RequestParam(value = "id", required = false) String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();
		if (key != null) {
			key = new StringBuffer("%").append(key).append("%").toString();
			conditions.put("key", key);
		}

		if (active != null)
			conditions.put("active", Boolean.parseBoolean(active));

		if (id != null)
			conditions.put("id", id);

		if (organizationID != null && !"role_org_root".equals(organizationID))
			conditions.put("organizationID", organizationID);

		if (buttonID != null)
			conditions.put("buttonID", buttonID);

		if (menuID != null)
			conditions.put("menuID", menuID);

		if (moduleID != null)
			conditions.put("moduleID", moduleID);

		if (operatorID != null)
			conditions.put("operatorID", operatorID);

		if (shortcutID != null)
			conditions.put("shortcutID", shortcutID);

		return bean.page(first, limit, sort, dir, conditions, null);
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Role.class, new JSONEditor(Role.class));
	}

}
